<template>
	<view>
		<view class="form_list">
			<view class="list-detail" @click="toPage('/pages/config/set')">
				<view><uni-icons type="gear" size="22"></uni-icons> 同步数据设置</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
		<view class="form_list">
			<view class="list-detail" @click="updateVersion">
				<view>
					<uni-icons type="cloud-upload-filled" size="22"></uni-icons> 
					版本更新 <span class="grep">{{online_version}}</span>
					<span class="dot" v-show="dot"></span>
				</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
		<view class="form_list">
			<view class="list-detail" @click="toPage('/pages/config/actionlog')">
				<view><uni-icons type="compose" size="22"></uni-icons> 操作日志</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
		<view class="form_list" v-if="isAdmin">
			<view class="list-detail" @click="toPage('/pages/config/userlist')">
				<view><uni-icons type="person-filled" size="22"></uni-icons> 用户列表</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
		<view class="form_list">
			<view class="list-detail" @click="toPage('/pages/config/login_info')">
				<view><uni-icons type="info" size="22"></uni-icons> 登录信息</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
		<view class="form_list">
			<view class="list-detail" @click="clearStorageSync">
				<view><uni-icons type="trash" size="22"></uni-icons> 清空缓存</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
		<view class="form_list">
			<view class="list-detail" @click="clearData">
				<view><uni-icons type="trash" size="22"></uni-icons> 清空数据</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
		<view class="form_list">
			<view class="list-detail" @click="tologout">
				<view><uni-icons type="undo" size="22"></uni-icons> 退出登录</view>
				<uni-icons type="forward" size="22"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
// 引入uni-ui组件
import {
  uniIcons
} from "@dcloudio/uni-ui"
export default {
  data() {
    return {
		listData:[],
		dot:false,
		isAdmin:false,
		myconfig: [],
		online_version:''
    }
  },
  components: {
	uniIcons
  },
  onLoad(){
	  
  },
  onShow(){
	  if(uni.getStorageSync('username') == 'admin'){
	  		  this.isAdmin = true
	  }
	  this.myconfig = uni.getStorageSync('myconfig')
	  this.setRedDot()
  },
  methods: {
    toPage(url){
		uni.showLoading({
			mask:true
		})
		uni.navigateTo({
			url:url,
		})
		uni.hideLoading()
    },
	//退出登录
	tologout:function(e){
		uni.showModal({
			title: "确定要退出登录?",
			content: "",
			success: (res) => {
				if (res.confirm) {
					uni.setStorageSync('user',null);
					
					uni.reLaunch({
						url:'/pages/index/login',
						success() {
							plus.navigator.closeSplashscreen()}
					})
				}
			},
		})
	},
	setRedDot:function(e){
		let _this = this
		let version = uni.getAppBaseInfo().appWgtVersion//uni.getStorageSync('version')//uni.getAppVersion().version;//
		if(this.myconfig.ourl!=''&&this.myconfig.ourl!=undefined){
			uni.request({
				url:this.myconfig.ourl+'/index.php/api/wms/getappversion',
				success: (vres) => {
					//if(vres.data.data.version!=version){
					_this.online_version = vres.data.data.version
					if(vres.data.data.version>version){
						this.dot  = true
					}
				},
			})
		}
	},
	updateVersion:function(e){
		let _this = this
		if(this.myconfig.ourl==''||this.myconfig.ourl==undefined){
			this.$Api.msg('请先设置外网地址')
			return 
		}
		
		if(!this.dot){
			this.$Api.msg('已是最新版','error')
			return
		}
		
		uni.request({
			url:this.myconfig.ourl+'/index.php/api/item/getappversion',
			success: (vres) => {
				if(this.dot){
					uni.showModal({
						title: "发现最新版本"+vres.data.data.version,
						content: "是否下载新版本",
						success: (res) => {
							if (res.confirm) {
								// this.doUpData()
								//开启下载任务
								/* uni.showToast({
									icon: 'success',
									mask: true,
									duration: 1000,
									title: "开始下载了",
								}) */
								uni.showLoading({
									title:'下载中...',
									mask:true
								})
								// #ifdef APP-PLUS
								var dtask = plus.downloader.createDownload(
									_this.myconfig.ourl+vres.data.data.apk,
									{
										method: "GET"
									}, (d, status) => {
										if (status == 200) {
											//console.log("下载成功安装: " + d.filename);
											plus.runtime.install(d.filename)
											setTimeout(()=>{
												_this.dot = false
												uni.setStorageSync('user',null);
												//uni.setStorageSync('version',vres.data.data.version)
												uni.hideLoading()
											},2000)
										} else {
											plus.nativeUI.alert("安装失败，请稍候重试: " + status)
										}
									});
								dtask.start();
								// #endif
							}else if (res.cancel) {
							  console.log('用户点击了取消按钮');
							}
						}
					})
				}
			},
			fail: function(e){
				uni.hideLoading()
				_this.$Api.msg('请联网再更新、网络错误')
			}
		});
	},
	 // 显示选择器
	showPicker() {
		this.$refs.treePicker._show();
	},
	//监听选择（ids为数组）
	selectChange(ids, names) {
		console.log(ids, names)
	},
	//清空本机所有缓存 
	clearStorageSync(){
		let _this = this
		uni.showModal({
			title: '温馨提醒',
			content: '确定要清空本机所有缓存？',
			success: function (res) {
				if (res.confirm) {
					_this.$Func.clearStorageSync()
				}
		}})
	},
	//清除数据
	clearData(){
		let _this = this
		uni.showModal({
			title: '温馨提醒',
			content: '确定要清空本地数据？',
			success: function (res) {
				if (res.confirm) {
					_this.$Db.clearData(_this.myconfig.delDbTab)
					.then(res=>{
						if(_this.myconfig.delDbTab){
							uni.showLoading({
								mask:true
							})
							setTimeout(()=>{
								uni.setStorageSync('backDataFlag',false)
								uni.hideLoading()
								_this.$Api.msg('数据、表已清除','success')
							},5000)
						}else{
							_this.$Api.msg('数据已清除','success')
						}
					})
					.catch(err=>{
						console.log(err)
						_this.$Api.msg(err.toString())
					})
				}
			}
		});
	},
  }
}
</script>
<style lang="less">
.dot {
  position: relative;
  width: 20rpx;
  height: 20rpx;
}

.dot::before {
  content: ' ';
  position: absolute;
  top: 10rpx;
  left: 0;
  width: 12rpx;
  height: 12rpx;
  background-color: red;
  border-radius: 50%;
  transform: translate(50%, -50%);
}
</style>